package com.zwj.batch.core.batch.step;

import com.zwj.batch.core.batch.step.processor.UserProcessor;
import com.zwj.batch.core.batch.step.reader.UserReader;
import com.zwj.batch.core.batch.step.tasklet.UserTasklet;
import com.zwj.batch.core.batch.step.writer.User2Writer;
import com.zwj.batch.core.batch.step.writer.UserWriter;
import com.zwj.batch.domain.po.UserPO;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.validator.BeanValidatingItemProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 张文接
 * @version 1.0
 * @createTime 2023-06-26 11:25:00
 */
@Component
public class UserStep {
    @Resource
    private StepBuilderFactory stepBuilderFactory;
    @Resource
    private UserReader userReader;
    @Resource
    private UserWriter userWriter;
    @Resource
    private User2Writer user2Writer;
    @Resource
    private BeanValidatingItemProcessor<UserPO> beanValidatingItemProcessor;
    @Resource
    private UserProcessor userProcessor;
    @Resource
    private UserTasklet userTasklet;

    public Step readUserTxtStep() {
        return stepBuilderFactory.get("readUserTxtStep")
                .<UserPO, UserPO>chunk(1)
                .reader(userReader.userItemReader("user.txt"))
                .writer(userWriter)
                .allowStartIfComplete(true)
                .build();
    }

    public Step readUserTxtStep2() {
        return stepBuilderFactory.get("readUserTxtStep2")
                .<UserPO, UserPO>chunk(1)
                .reader(userReader.userItemReader2())
                .writer(user2Writer)
                .allowStartIfComplete(true)
                .build();
    }

    public Step readUserJsonStep() {
        return stepBuilderFactory
                .get("readUserJsonStep")
                .<UserPO, UserPO>chunk(1)
                .reader(userReader.userItemReader3())
                .writer(userWriter)
                .allowStartIfComplete(true)
                .build();
    }

    public Step readUserJsonValidateStep() {
        return stepBuilderFactory
                .get("readUserJsonValidateStep")
                .<UserPO, UserPO>chunk(1)
                .reader(userReader.userItemReader("users-validate.txt"))
                .processor(beanValidatingItemProcessor) //字段校验
                .writer(userWriter)
                .allowStartIfComplete(true)
                .build();
    }

    public Step getProcessUserStep() {
        return stepBuilderFactory
                .get("adapter-processor-job")
                .<UserPO, UserPO>chunk(1)
                .reader(userReader.userItemReader("user.txt"))
                .processor(userProcessor.userProcessor())
                .writer(userWriter)
                .build();
    }

    public Step getCompositeProcessUserStep() {
        return stepBuilderFactory
                .get("composite-processor-step")
                .<UserPO, UserPO>chunk(1)
                .reader(userReader.userItemReader("users-validate.txt"))
                .processor(userProcessor.compositeItemProcessor())
                .writer(userWriter)
                .build();
    }

    public Step getCustomizeProcessUserStep() {
        return stepBuilderFactory
                .get("customize-processor-job")
                .<UserPO, UserPO>chunk(1)
                .reader(userReader.userItemReader("user.txt"))
                .processor(userProcessor.customizeItemProcessor())
                .writer(userWriter)
                .build();
    }

    /**
     * 学习batchJob用法的step
     *
     * @return Step
     */
    public Step batchJobStudy() {
        return stepBuilderFactory
                .get("batchJobStudyStep")
                .tasklet(userTasklet.batchJobStudyTasklet())
                .build();
    }
}
